import axios from 'axios';
import { CronJob } from 'cron';

function getRandomData(arr, count) {
  let shuffled = arr.slice(0);
  let i = arr.length;
  let min = i - count;
  let temp;
  let index;
  while (i-- > min) {
    index = Math.floor((i + 1) * Math.random());
    temp = shuffled[index];
    shuffled[index] = shuffled[i];
    shuffled[i] = temp;
  }
  return shuffled.slice(min);
}


const solarList = [
  '立春', '雨水', '惊蛰', '春分', '清明', '谷雨',
  '立夏', '小满', '芒种', '夏至', '小暑', '大暑',
  '立秋', '处暑', '白露', '秋分', '寒露', '霜降',
  '立冬', '小雪', '大雪', '冬至', '小寒', '大寒'
]
const colors = {
  立春: [
    '#ECD452', '#B6A014',
    '#D5EBE1', '#B1D5C8', '#99BCAC', '#80A492',
    '#8B7042', '#775039', '#5F4321', '#422517',
    '#F3A694', '#EE7959', '#BA5140', '#C12C1F'

  ],
  雨水: [
    '#F9D3E3', '#ECB0C1', '#DD7694', '#A76283',
    '#BEB1AA', '#B49273', '#A46244', '#6B5458',
    '#E5A84B', '#E18A3B', '#984F31', '#7C461E',
    '#C0D695', '#A9BE7B', '#779649', '#4F6F46'
  ],
  惊蛰: [
    '#BA5B49', '#A64036', '#9E2A22', '#7C191E',
    '#F6BEC8', '#F091A0', '#DC6B82', '#C35C5D',
    '#FEDC5E', '#FAC03D', '#DB9B34', '#C67915',
    '#6B798E', '#45465E', '#2C2F3B'
  ],
  春分: [
    '#D2AF9D', '#BC836B', '#B26D5D', '#9A6655',
    '#EA5514', '#D23918', '#C8161D', '#A72126',
    '#3271AE', '#007175', '#284852', '#12264F'
  ],
  清明: [
    '#A6559D', '#8A1874', '#6C216D', '#422256',
    '#9D9D82', '#919177', '#79836C',
    '#D3CCD6', '#9B8EA9', '#7E527F', '#663D74',
    '#CB5C83', '#B83570', '#A73766', '#903754'
  ],
  谷雨: [
    '#DCC7E1', '#BBA1CB', '#A67EB7', '#7D5284',
    '#A8BF8F', '#68945C', '#4C8045', '#446A37',
    '#AED0EE', '#6E9BC5', '#354E6B', '#13393E',
    '#BD8253', '#945635', '#804C2E', '#5B3222'
  ],
  立夏: [
    '#C3D94E', '#B7D332', '#9EBC19', '#84A729',
    '#DFCEB4', '#BFA782', '#B49B7F', '#94784F',
    '#BEC2BC', '#A8A19C', '#92897B', '#70695D',
    '#F29A76', '#ED6D3D', '#E94829', '#E60012'
  ],
  小满: [
    '#E2A2AC', '#DD6B7B', '#C25160', '#B81A35',
    '#6A8D52', '#5D8351', '#4F794A', '#2A6E3F',
    '#D4C9AA', '#B4A379', '#9E8358', '#81663B',
    '#F2C867', '#D3A237', '#D08635', '#9F6027'
  ],
  芒种: [
    '#D5D1AE', '#BFC096', '#A9A886', '#92905D',
    '#B3B59C', '#A8B092', '#A9A773', '#868C4E',
    '#DDBB99', '#D2A36C', '#BE9457', '#B4884D',
    '#535164', '#454659', '#343041', '#1E2732'
  ],
  夏至: [
    '#CB523E', '#B13B2E', '#95302E', '#822327',
    '#B2B6B6', '#A7AAA1', '#7A7B78', '#686A67',
    '#F0C2A2', '#DB9C5E', '#D9883D', '#CC5D20',
  ],
  小暑: [
    '#F5B087', '#F18F60', '#EF845D', '#ED6D46',
    '#E0DFC6', '#BFB99C', '#A99F70', '#877D52',
    '#954024', '#873424', '#662B1F', '#4C1E1A',
    '#106898', '#12507B', '#06436F', '#003460'
  ],
  大暑: [
    '#E3ADB9', '#CF929E', '#B27777', '#A35F65',
    '#E3EB98', '#D8DE8A', '#CDD171',
    '#D4BF89', '#BFA670', '#AA8E59', '#896D47',
    '#BED2BB', '#9BB496', '#81A380', '#698E6A'
  ],
  立秋: [
    '#88ABDA', '#6F94CD', '#5976BA', '#2E59A7',
    '#98B6C2', '#7F9FAF', '#66889E', '#547689',
    '#D8D1C5', '#C6BEB1', '#AC9F8A',
    '#C0D09D', '#938F4C', '#7F754C', '#595333'
  ],
  处暑: [
    '#F0CFE3', '#E4B8D5', '#CE93BF', '#BA79B1',
    '#C9CFC1', '#A8B78C', '#90A07D', '#6C8650',
    '#A2D2E2', '#87C0CA', '#5AA4AE', '#108B96',
    '#EAD89A', '#D5B45C', '#BF9C46', '#AA8534'
  ],
  白露: [
    '#DFD6B8', '#D5C8A0',
    '#86908A', '#6B7D73', '#555F4D', '#45493D',
    '#D3CBC5', '#C8B5B3', '#928187', '#594C57',
    '#C4B798', '#A58A5F', '#7C623F', '#644D31'
  ],
  秋分: [
    '#D5E3D4', '#CAD7C5', '#BECAB7', '#B3BDA9',
    '#C0AD5E', '#AA9649', '#8F3D2C', '#683632',
    '#4994C4', '#4182A4', '#32788A', '#006D87',
    '#EAEEF1', '#BDCBD2', '#B2BFC3'
  ],
  寒露: [
    '#A6BAB1', '#819D8E', '#778A77', '#5F766A',
    '#DDB078', '#DA9233', '#BC6E37', '#9F5221',
    '#8BA3C7', '#003D74', '#19325F', '#1A2847',
    '#E1C199', '#C6A268', '#B3934B'
  ],
  霜降: [
    '#D12920', '#AB1D22', '#8F1D22', '#631216',
    '#BDB2B2', '#91828F', '#6A5B6D', '#5C4F55',
    '#DA9E8C', '#D77F66', '#B15A43',
    '#DFD7C2', '#D4C1A6', '#BEA78B', '#99806C'
  ],
  立冬: [
    '#FFFBC7', '#F7EEAD', '#ECE093', '#D6C560',
    '#88BFB8', '#5DA39D', '#3D8E86', '#206864',
    '#9E8C6B', '#887657', '#6D5C3D', '#5A4B3B',
    '#A88787', '#9E696D', '#81474C', '#662B2F'
  ],
  小雪: [
    '#DE82A7', '#CC73A0', '#B95A89', '#B0436F',
    '#E67762', '#DD6B4F', '#D24735', '#B93A26',
    '#BCD4E7', '#A3BBDB', '#8AABCC',
    '#9E8368', '#906C4A', '#7C5B3E', '#72453A'
  ],
  大雪: [
    '#EFC4CE', '#CE8892', '#C35C6A', '#B04552',
    '#BFC1A9', '#A9A082', '#756C4B',
    '#788A6F', '#5D7259', '#4E6548', '#3F503B',
    '#A4ABD6', '#757CBB', '#615EA8', '#4A4B9D'
  ],
  冬至: [
    '#E7CAD3', '#D9A0B3', '#BB7A8C', '#9E4E56',
    '#DAA9A9', '#CD7372', '#A85858', '#7C4449',
    '#EBE1A9', '#E1D279', '#D6BC46', '#BE8A2F',
    '#796860', '#585248', '#46433B', '#31322C'
  ],
  小寒: [
    '#ECEBC2', '#E1D384', '#CAB272',
    '#BD9683', '#955A42', '#81492C', '#68361A',
    '#7D929F', '#576470', '#3C4654', '#151D29',
    '#A4C9CC', '#6CA8AF', '#509296', '#226B68'
  ],
  大寒: [
    '#995D7F', '#814662', '#602641', '#420B2F',
    '#CAC5A0', '#92896B', '#665F4D',
    '#DDC5B8', '#BEA89D', '#8F6D5F', '#694B3C',
    '#C8B6BB', '#9C8D9B', '#9D858F', '#76555D'
  ]
}

function startSchedule() {
  let job = CronJob.from({
    // cronTime: '0 0 0 * * *',
    // cronTime: '* * * * *', // 每分钟
    cronTime: '0 8 * * *', // 每天上午8点
    // cronTime: '*/10 * * * * *',
    onTick: function () {
      start();
    },
    start: true,
    timeZone: 'Asia/Shanghai'
  });
}
startSchedule();
async function start() {
  console.log('start')
  const weather = await axios.get('https://api.caiyunapp.com/v2.6/LS73tkajKuQ7GieR/119.9814,31.8157/weather?dailysteps=1')
  console.log(weather.data)
  const result = weather.data.result
  const daily = result.daily
  const temperature = result.daily.temperature
  const skyconMap = {
    CLEAR_DAY: '晴（白天）',
    CLEAR_NIGHT: '晴（夜间）',
    PARTLY_CLOUDY_DAY: '多云（白天）',
    PARTLY_CLOUDY_NIGHT: '多云（夜间）',
    LIGHT_HAZE: '轻度雾霾',
    MODERATE_HAZE: '中度雾霾',
    HEAVY_HAZE: '重度雾霾',
    CLOUDY: '阴',
    LIGHT_RAIN: '小雨',
    MODERATE_RAIN: '中雨',
    HEAVY_RAIN: '大雨',
    STORM_RAIN: '暴雨',
    FOG: '雾',
    LIGHT_SNOW: '小雪',
    MODERATE_SNOW: '中雪',
    HEAVY_SNOW: '大雪',
    STORM_SNOW: '暴雪',
    DUST: '浮尘',
    SAND: '沙尘',
    WIND: '大风'
  }

  const iciba = await axios.get('https://open.iciba.com/dsapi/')
  console.log(iciba.data)

  const taurus = await axios.get(`http://web.juhe.cn/constellation/getAll?key=611c5ceb91c375e78f3fe463108392c9&consName=金牛座&type=today`)
  console.log(taurus.data);

  const pisces = await axios.get(`http://web.juhe.cn/constellation/getAll?key=611c5ceb91c375e78f3fe463108392c9&consName=双鱼座&type=today`)
  console.log(pisces.data);

  const soup = await axios.get('https://apis.juhe.cn/fapig/soup/query?key=719999caa583b274623ac86b242a504d')

  const randomSolar = getRandomData(solarList, 6)
  const randomColor = getRandomData(colors[randomSolar[0]], 8)
  const randomColorTitle = getRandomData(colors[randomSolar[1]], 4)
  const randomColorWord = getRandomData(colors[randomSolar[2]], 2)

  const randomColorStar = getRandomData(colors[randomSolar[3]], 10)
  const randomColorStar02 = getRandomData(colors[randomSolar[4]], 10)
  const randomSoup = getRandomData(colors[randomSolar[5]], 1)

  const res = await axios.post('https://wxpusher.zjiecode.com/api/send/message', {
    "appToken": "AT_H6umbReGK8PqmdrLtZXbL9aRA34Z1rQV",
    "content": `
    <div style="color:#1E2732;position:relative;">
      <img style="position:absolute;top:-20px;right:0;width:80px;height:80px;" src="https://img.alicdn.com/imgextra/i1/732742758/O1CN01JWdU0y1WFDKBx1nfO_!!732742758.png" />
      <h2 style="color:${randomColorTitle[0]};">每日一句</h2>
    <img style="width:100%;" src=${iciba.data.fenxiang_img} />
    <p style="color:${randomColorWord[0]};">${iciba.data.content}</p>
    <p>${iciba.data.note}</p>
    <div style="position:relative;">
      <img style="position:absolute;top:0px;right:0;width:140px;height:auto;" src="https://img.alicdn.com/imgextra/i4/732742758/O1CN01hNvHLz1WFDKEOAAQ6_!!732742758.png" />
    <h2 style="color:${randomColorTitle[1]};padding-top:16px;border-top:1px dotted #EBEDDF">每日天气</h2>
    <p>今日天气：
    <span style="color:${randomColor[0]};font-weight: 600;">${skyconMap[daily.skycon[0].value]}</span>
    </p>
    <p>日出时间：
    <span style="color:${randomColor[1]};">${daily.astro[0].sunrise.time}</span>
    ，日落时间：
    <span style="color:${randomColor[2]};">${daily.astro[0].sunset.time}</span>
    </p>
    <p>全天最低气温：
    <span style="color:${randomColor[3]};">${temperature[0].min}℃ </span>
    ，全天最高气温：
    <span style="color:${randomColor[4]};">${temperature[0].max}℃</span>
    </p>
    <p>生活指数：
      <p style="margin-left:20px;">-- 紫外线指数: <span style="color:${randomColor[5]};">${daily.life_index.ultraviolet[0].desc}</span></p>
      <p style="margin-left:20px;">-- 穿衣指数: <span style="color:${randomColor[5]};">${daily.life_index.dressing[0].desc}</span></p>
      <p style="margin-left:20px;">-- 舒适度指数: <span style="color:${randomColor[5]};">${daily.life_index.comfort[0].desc}</span></p>
      <p style="margin-left:20px;">-- 感冒指数: <span style="color:${randomColor[5]};">${daily.life_index.coldRisk[0].desc}</span></p>
    </p>
    <p>未来趋势：
    <span style="color:${randomColor[6]};">${result.forecast_keypoint}</span>
    </p>
      </div>
       <div style="position:relative;">
         <img style="position:absolute;top:10px;right:0px;width:100px;height:auto;" src="https://img.alicdn.com/imgextra/i1/732742758/O1CN01lY4Chb1WFDKGQo5XQ_!!732742758.png" />
     <h2 style="color:${randomColorTitle[2]};padding-top:16px;border-top:1px dotted #EBEDDF">今日运势 <span style="font-size:14px;color:#1E2732;">${taurus.data.datetime}</span></h2>
     <h3 style="margin-left:10px;color:${randomColorStar[0]};">金牛座</h3>
     <div style="margin-left:20px;">
        <p>综合指数: <span style="color:${randomColorStar[1]};">${taurus.data.all}</span></p>
        <p>健康指数: <span style="color:${randomColorStar[2]};">${taurus.data.health}</span></p>
        <p>爱情指数: <span style="color:${randomColorStar[3]};">${taurus.data.love}</span></p>
        <p>财运指数: <span style="color:${randomColorStar[4]};">${taurus.data.money}</span></p>
        <p>工作指数: <span style="color:${randomColorStar[5]};">${taurus.data.work}</span></p>
        <p>幸运数字: <span style="color:${randomColorStar[6]};">${taurus.data.number}</span></p>
        <p>幸运色: <span style="color:${randomColorStar[7]};">${taurus.data.color}</span></p>
        <p>速配星座: <span style="color:${randomColorStar[8]};">${taurus.data.QFriend}</span></p>
        <p style="line-height:1.8;font-size:14px;color:${randomColorStar[9]};">${taurus.data.summary}<p>
      </div>
      <h3 style="margin-left:10px;color:${randomColorStar02[0]};">双鱼座</h3>
     <div style="margin-left:20px;">
        <p>综合指数: <span style="color:${randomColorStar02[1]};">${pisces.data.all}</span></p>
        <p>健康指数: <span style="color:${randomColorStar02[2]};">${pisces.data.health}</span></p>
        <p>爱情指数: <span style="color:${randomColorStar02[3]};">${pisces.data.love}</span></p>
        <p>财运指数: <span style="color:${randomColorStar02[4]};">${pisces.data.money}</span></p>
        <p>工作指数: <span style="color:${randomColorStar02[5]};">${pisces.data.work}</span></p>
        <p>幸运数字: <span style="color:${randomColorStar02[6]};">${pisces.data.number}</span></p>
        <p>幸运色: <span style="color:${randomColorStar02[7]};">${pisces.data.color}</span></p>
        <p>速配星座: <span style="color:${randomColorStar02[8]};">${pisces.data.QFriend}</span></p>
        <p style="line-height:1.8;font-size:14px;color:${randomColorStar02[9]};">${pisces.data.summary}<p>
    </div>
    </div>
        <h2 style="color:${randomColorTitle[3]};padding-top:16px;border-top:1px dotted #EBEDDF">今日鸡汤</h2>
     <p style="line-height:1.8;font-size:14px;color:${randomSoup[0]};text-indent:2em;">${soup.data.result.text}<p>
    <h3 style="text-align:center;color:${randomColor[7]};">小陈今天要棒棒哒!</h3>

    <img style="position:absolute;bottom:10px;right:0;width:130px;" src="https://img.alicdn.com/imgextra/i4/732742758/O1CN01co1otj1WFDK7Rwqir_!!732742758.png" />
    </div>
    `,
    "summary": "小贺每日提醒",
    "contentType": 2,
    "uids": [
      "UID_STLYLM2qcOoqGwt9F1FL2Z5QyviU",
      "UID_l6Sk9r59jl1oIPHCQxVGzoFpTAEL"
    ],
    "url": "",
    "verifyPay": false,
    "verifyPayType": 0
  })
}
